package edu.chinasoft.dao;

import edu.chinasoft.pojo.Book;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;


public
class BookDaoImp implements BookDao{
    @Override
    public
    void insertTable(Book book) {
        //步骤1：加载mybatis框架的配置文件
        String resource="mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream( resource );
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build( inputStream );
        //步骤2：获取数据库连接对象  sqlSession    connection
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //步骤3：执行sql语句
        int number = sqlSession.insert( "edu.chinasoft.dao.BookDao.insertTable",book );
        //手动提交事务
        sqlSession.commit();
        if (number>0){
            System.out.println("执行成功！");
        }else {
            System.out.println("执行失败！");
        }
        //步骤4：关闭连接
        sqlSession.close();
    }
}
